package com.tust.androidreader;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.tust.androidreader.Adapter.LibraryRecyclerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LibraryActivity extends AppCompatActivity {
    private Toast toast;
    private List<Map<String,Object>> bookList;
    private List<Map<String,Object>> searchBookList;
    private String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();//隐藏标题导航栏
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去除时间和电量等状态栏
        setContentView(R.layout.activity_library);

        DBHelper dbHelper = new DBHelper(this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        Cursor bookCursor = db.rawQuery("select * from book", null);
        bookList = initDatas(bookCursor);

		RecyclerView recycelerViewLibrary = this.findViewById(R.id.rv_library);
        EditText editTextSearchBar = this.findViewById(R.id.et_searchbar);
        ImageView imageViewSearch = this.findViewById(R.id.iv_search);
        FloatingActionButton floatingActionButton = this.findViewById(R.id.fab);

        userName = this.getIntent().getStringExtra("username");

        LibraryRecyclerAdapter libraryRecyclerAdapter = new LibraryRecyclerAdapter(this,bookList,userName);
		LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
		linearLayoutManager.setReverseLayout(false);

		recycelerViewLibrary.setLayoutManager(linearLayoutManager);
		recycelerViewLibrary.setAdapter(libraryRecyclerAdapter);

        editTextSearchBar.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {//获得焦点
                    if (editTextSearchBar.getText().toString().equals("查询书名或作者")) {
                        editTextSearchBar.setText("");
                    }
                } else {
                    if (editTextSearchBar.getText().toString().equals("")) {
                        editTextSearchBar.setText("查询书名或作者");
                    }
                }
            }
        });

        imageViewSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String searchBarContent = "%" + editTextSearchBar.getText().toString() + "%";

                if (searchBarContent.equals("")) {
                    recycelerViewLibrary.setAdapter(libraryRecyclerAdapter);//恢复到显示所有book
                    showToast("搜索框为空！");
                } else  if (searchBarContent.equals("查询书名或作者")) {
                    recycelerViewLibrary.setAdapter(libraryRecyclerAdapter);
                    showToast("请输入查询内容！");
                } else {//搜索功能
                    Cursor booknameCursor = db.rawQuery("select * from book where bookname like ?", new String[]{searchBarContent});

                    if (booknameCursor.getCount() < 1) {//没有找到该书名
                        Cursor authorCursor = db.rawQuery("select * from book where author like ?", new String[]{searchBarContent});

                        if (authorCursor.getCount() < 1) {//没有找到该作者
                            showToast("没有找到该书！");
                        } else {//通过作者产生list
                            searchBookList = initDatas(authorCursor);

                            LibraryRecyclerAdapter searchLibraryRecyclerAdapter = new LibraryRecyclerAdapter(LibraryActivity.this,searchBookList,userName);
                            recycelerViewLibrary.setAdapter(searchLibraryRecyclerAdapter);
                        }

                        authorCursor.close();
                    } else {//通过书名产生list
                        searchBookList = initDatas(booknameCursor);

                        LibraryRecyclerAdapter searchLibraryRecyclerAdapter = new LibraryRecyclerAdapter(LibraryActivity.this,searchBookList,userName);
                        recycelerViewLibrary.setAdapter(searchLibraryRecyclerAdapter);
                    }

                    booknameCursor.close();
                }
            }
        });

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LibraryActivity.this,AddBookActivity.class);
                startActivity(intent);
            }
        });
    }

    private  List<Map<String,Object>> initDatas(Cursor cursor) {
        List<Map<String,Object>> list = new ArrayList<>();

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {//游标只要不是在最后一行之后，就一直循环
            Map<String,Object> item = new HashMap<>();

            item.put("bookicon",cursor.getInt(0));
            item.put("bookname",cursor.getString(1));
            item.put("author",cursor.getString(2));
            item.put("likenum",cursor.getString(3));
            item.put("readtimes",cursor.getString(4));
            item.put("commentnum",cursor.getString(5));
            item.put("classify",cursor.getString(6));
            item.put("addtime",cursor.getString(7));
            item.put("wordnum",cursor.getString(8));

            list.add(item);

            cursor.moveToNext();//将游标移到下一行
        }

        cursor.close();
        return list;
    }
    
    @Override
    public void onBackPressed() {//禁止返回
        //super.onBackPressed();
    }

    private void showToast(String s) {
        if (toast == null){
            toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
        }else {
            toast.cancel();
            toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
        }
        toast.show();
    }
}